<?php

namespace addons\shopro\controller;

use addons\shopro\model\Cart as CartModel;
use addons\shopro\model\User;

class Cart extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $data = CartModel::info();
        $this->success('我的购物车', $data);
    }

    public function add()
    {
        $params = $this->request->post();
        
        // 表单验证
        $this->shoproValidate($params, get_class(), 'add');
        /*$goods_list = [
            [
                'goods_id' => 18,
                'goods_num' => 3,
                'sku_price_id' => 83,],
            [
                'goods_id' => 22,
                'goods_num' => 2,
                'sku_price_id' => 94,
            ]
        ];
        $params['goods_list'] = $goods_list;*/
        $goodsList = $params['goods_list'];
        $this->success('已添加', CartModel::add($goodsList));
    }

    public function edit()
    {
        $params = $this->request->post();
//        $params['cart_list'] = [11];
        // 表单验证
        $this->shoproValidate($params, get_class(), 'edit');

        $this->success('操作成功', CartModel::edit($params));
    }

    public function select_all()
    {
        $is_select = $this->request->post('is_select', -1);

        if(in_array($is_select, [0, 1])){
            db('shopro_cart')->where('user_id', User::info()->id)->update(['is_select' => $is_select]);
        }
        $this->success('操作成功');
    }

}